# Author    :Dun_Hz
# Time      :2024/3/18 9:59
# 节点与引用

class BinaryTree:
    def __init__(self, root):
        self.root = root
        self.left_child = None
        self.right_child = None

    # 插入左子节点
    def insert_left(self, new_node):
        # 左子节点为空，直接添加节点
        if self.left_child == None:
            self.left_child = BinaryTree(new_node)
        # 已经存在左子节点，插入节点，将原来左子节点降一层
        else:
            new_child = BinaryTree(new_node)
            new_child.left_child = self.left_child
            self.left_child = new_child

    # 插入右子节点
    def insert_right(self, new_node):
        if self.right_child is None:
            self.right_child = BinaryTree(new_node)
        else:
            new_child = BinaryTree(new_node)
            new_child.right_child = self.right_child
            self.right_child = new_child

    # 二叉树的访问
    def get_root_val(self):
        return self.root
    def set_root_val(self, new_root):
        self.root = new_root
    def get_left_child(self):
        return self.left_child
    def get_right_child(self):
        return self.right_child

if __name__ == '__main__':
    a_tree = BinaryTree('a')
    root = a_tree.get_root_val()
    print(root)
    print(a_tree.get_left_child())
    a_tree.insert_left('b')
    print(a_tree.get_left_child().get_root_val())
    a_tree.insert_right('c')
    print(a_tree.get_right_child().get_root_val())
    a_tree.get_right_child().set_root_val('hello')
    print(a_tree.get_right_child().get_root_val())